import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:azlistview/azlistview.dart';

class CnCountryPage extends StatefulWidget {
  const CnCountryPage({super.key});

  @override
  State<CnCountryPage> createState() => _countryPageState();
}

class _countryPageState extends State<CnCountryPage> {
  List<CityModel> cityList = [];
  var countryDict = {
    "A": [
      "Afghanistan",
      "Albania",
      "Algeria",
      "American Samoa",
      "Andorra",
      "Angola",
      "Anguilla",
      "Antigua and Barbuda",
      "Argentina",
      "Armenia",
      "Aruba",
      "Australia",
      "Austria",
      "Azerbaijan"
    ],
    "B": [
      "Bahamas",
      "Bahrain",
      "Bangladesh0",
      "Barbados",
      "Belarus",
      "Belgium",
      "Belize",
      "Benin",
      "Bermuda",
      "Bhutan",
      "Bolivia",
      "Bosnia and Herzegovina",
      "Botswana",
      "Brazil",
      "British Indian Ocean Territory",
      "Brunei Darussalam",
      "Bulgaria",
      "Burkina Faso",
      "Burundi"
    ],
    "C": [
      "Cambodia",
      "Cameroon",
      "Canada",
      "Cape Verde",
      "Cayman Islands",
      "Central African Republic",
      "Chad",
      "Chile",
      "China",
      "Colombia",
      "Comoros",
      "Congo - Brazzaville",
      "Congo - Kinshasa",
      "Cook Islands",
      "Costa Rica",
      "Côte d'Ivoire",
      "Croatia",
      "Cuba",
      "Cyprus",
      "Czech Republic"
    ],
    "D": [
      "Denmark",
      "Djibouti",
      "Dominica",
      "Dominican Republic"
    ],
    "E": [
      "Ecuador",
      "Egypt",
      "El Salvador",
      "Equatorial Guinea",
      "Eritrea",
      "Estonia",
      "Ethiopia"
    ],
    "F": [
      "Falkland Islands",
      "Faroe Islands",
      "Fij",
      "Finland",
      "France",
      "French Guiana",
      "French Polynesia",
      "French Southern Territories"
    ],
    "G": [
      "Gabon",
      "Gambia",
      "Georgia",
      "Germany",
      "Ghana",
      "Gibraltar",
      "Greece",
      "Greenland",
      "Grenada",
      "Guadeloupe",
      "Guam",
      "Guatemala",
      "Guernsey",
      "Guinea",
      "Guinea-Bissau",
      "Guyana"
    ],
    "H": [
      "Haiti",
      "Vatican City",
      "Hondura",
      "Hong Kong",
      "Hungary"
    ],
    "I": [
      "Iceland",
      "India",
      "Indonesia",
      "Iran",
      "Iraq",
      "Ireland",
      "Israel",
      "Italy"
    ],
    "J": ["Jamaica", "Japan", "Jersey", "Jordan"],
    "K": [
      "Kazakhstan",
      "Kenya",
      "Kiribati",
      "Kuwait",
      "Kyrgyzstan"
    ],
    "L": [
      "Laos",
      "Latvia",
      "Lebanon",
      "Lesotho",
      "Liberia",
      "Libya",
      "Liechtenstein",
      "Lithuania",
      "Luxembourg"
    ],
    "M": [
      "Macao",
      "Macedonia",
      "Madagascar",
      "Malawi",
      "Malaysia",
      "Maldives",
      "Mali",
      "Malta",
      "Marshall Islands",
      "Martinique",
      "Mauritania",
      "Mauritius",
      "Mayotte",
      "Mexico",
      "Micronesia",
      "Moldova",
      "Monaco",
      "Mongolia",
      "Montenegro",
      "Montserrat",
      "Morocco",
      "Mozambique",
      "Myanmar"
    ],
    "N": [
      "Namibia",
      "Nauru",
      "Nepal",
      "Netherlands",
      "New Caledonia",
      "New Zealand",
      "Nicaragua",
      "Niger",
      "Nigeria",
      "Niue",
      "Norfolk Island",
      "North Korea",
      "Northern Mariana Islands",
      "Norway"
    ],
    "O": ["Oman"],
    "P": [
      "Pakistan",
      "Palau",
      "Palestinian Territories",
      "Panama",
      "Papua New Guinea",
      "Paraguay",
      "Peru",
      "Philippines",
      "Poland",
      "Portugal",
      "Puerto Rico"
    ],
    "Q": ["Qatar"],
    "R": ["Romania", "Russia", "Rwanda", "Réunion"],
    "S": [
      "Saint Barthélemy",
      "Saint Helena",
      "Saint Kitts and Nevis",
      "Saint Lucia",
      "Saint Martin",
      "Saint Pierre and Miquelon",
      "Saint Vincent and the Grenadines",
      "Samoa",
      "San Marino",
      "São Tomé and Príncipe",
      "Saudi Arabia",
      "Senegal",
      "Serbia",
      "Seychelles",
      "Sierra Leone",
      "Singapore",
      "Slovakia",
      "Slovenia",
      "Solomon Islands",
      "Somalia",
      "South Africa",
      "South Korea",
      "Spain",
      "Sri Lanka",
      "Sudan",
      "Suriname",
      "Svalbard and Jan Mayen",
      "Swaziland",
      "Sweden",
      "Switzerland",
      "Syria"
    ],
    "T": [
      "Taiwan",
      "Tajikistan",
      "Tanzania",
      "Thailand",
      "Timor-Leste",
      "Togo",
      "Tokelau",
      "Tonga",
      "Trinidad and Tobago",
      "Tunisia",
      "Turkey",
      "Turkmenistan",
      "Turks and Caicos Islands",
      "Tuvalu"
    ],
    "U": [
      "Uganda",
      "Ukraine",
      "United Arab Emirates",
      "United Kingdom",
      "United States",
      "Uruguay",
      "Uzbekistan"
    ],
    "V": [
      "Vanuatu",
      "Venezuela",
      "Vietnam",
      "Virgin Islands, British",
      "Virgin Islands, U.S."
    ],
    "W": ["Wallis and Futuna"],
    "Y": ["Yemen"],
    "Z": ["Zambia", "Zimbabwe"]
  };

  @override
  void initState() {
    super.initState();
    countryDict.forEach((key, value) {
      for (var city in value) {
        cityList.add(CityModel(name: city, namePinyin: 'A', tagIndex: key));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Theme.of(context).colorScheme.inversePrimary,
          leading: BackButton(color: Colors.black),
          title: const Text(
            'Country',
            style: TextStyle(color: Colors.black, fontSize: 18),
          ),
        ),
        body: SafeArea(
            child: Column(
          children: [
            Expanded(
              child: AzListView(
                data: cityList,
                itemCount: cityList.length,
                itemBuilder: (BuildContext context, int index) {
                  CityModel model = cityList[index];
                  return Utils.getListItem(context, model);
                },
                padding: EdgeInsets.zero,
                susItemBuilder: (BuildContext context, int index) {
                  CityModel model = cityList[index];
                  String tag = model.getSuspensionTag();
                  return Utils.getSusItem(context, tag);
                },
                indexBarData: ['★', ...kIndexBarData],
              ),
            ),
          ],
        )));
  }
}

class CityModel extends ISuspensionBean {
  String? name;
  String? tagIndex;
  String? namePinyin;

  CityModel({
    this.name,
    this.tagIndex,
    this.namePinyin,
  });

  CityModel.fromJson(Map<String, dynamic> json) : name = json['name'];

  Map<String, dynamic> toJson() => {
        'name': name,
      };

  @override
  String getSuspensionTag() => tagIndex!;

  @override
  String toString() => json.encode(this);
}

class Utils {
  static Widget getSusItem(BuildContext context, String tag,
      {double susHeight = 40}) {
    if (tag == '★') {
      tag = '★ 热门城市';
    }
    return Container(
      height: susHeight,
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.only(left: 16.0),
      color: Color(0xFFF3F4F5),
      alignment: Alignment.centerLeft,
      child: Text(
        '$tag',
        softWrap: false,
        style: const TextStyle(
          fontSize: 14.0,
          color: Color(0xFF666666),
        ),
      ),
    );
  }

  static Widget getListItem(BuildContext context, CityModel model,
      {double susHeight = 40}) {
    return ListTile(
      title: Text(
        model.name!,
      ),
      onTap: () {
        Navigator.pop(context, model.name);
      },
    );
  }
}
